﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AlphaD.Core.Functions
{
    public class LinearDiffEq2BvpFunc : BaseSegFunction<double, double>
    {
        private readonly double _k;
        private readonly double _r;
        private double _f;
        private double _c1;
        private double _c2;

        private readonly double _r_k;
        private double _f_r;

        public LinearDiffEq2BvpFunc(double left, double right, double k, double r, double f, double c1, double c2)
            : base(left, right)
        {
            this._k = k;
            this._r = r;
            this._f = f;
            this._c1 = c1;
            this._c2 = c2;

            this._r_k = Math.Sqrt(this._r / this._k);
            this._f_r = this._f / this._r;
        }

        public void AddTo(LinearDiffEq2BvpFunc f)
        {
            if (this._r_k != f._r_k)
            {
                throw new ArgumentException("Functions can not be added because sqrt(r / k) are not equal");
            }

            this._c1 += f._c1;
            this._c2 += f._c2;
            this._f += f._f;

            this._f_r = this._f / this._r;
        }

        protected override double CalcValue(double x)
        {
            return _c1 * Math.Exp(_r_k * x) + _c2 * Math.Exp(-_r_k * x) + _f_r;
        }
    }

}
